package com.ruoyi.controller;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.domain.ExceptionData;
import com.ruoyi.dto.ExceptionDataDTO;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.service.ExceptionDataService;
import com.ruoyi.vo.ExceptionDataVO;
import com.ruoyi.vo.SkuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 异常数据页面接口
 * @Author:zzm
 * @Date: 2024/10/22 11:14
 */
@RestController
@RequestMapping("/amac/exceptionalData")
public class ExceptionDataController extends BaseController {

    @Autowired
    private ExceptionDataService exceptionDataService;
    @Autowired
    private ServerConfig serverConfig;

    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws IOException{
        if (!file.isEmpty()){
            String filePath = RuoYiConfig.getUploadPath();
            //上传并返回新文件名称（即你存的文件名称）（带路径）
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            if (!StringUtils.isEmpty(url)){
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", url);
                return ajax;
            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }

    /**
     * 新增
     */
    @PostMapping
    public Boolean add(@RequestBody ExceptionDataDTO dto){
        return exceptionDataService.add(dto);

    }


    /**
     * 修改
     * @param dto
     * @return
     */
    @PutMapping
    public Boolean update(@RequestBody ExceptionDataDTO dto){
        return exceptionDataService.update(dto);
    }

    /**
     * 查询
     * @param dto
     * @return
     */
    @GetMapping("/getList")
    public TableDataInfo getList(ExceptionData dto){
        startPage();
        return getDataTable(exceptionDataService.getList(dto));
    }

    /**
     * 获取详情
     * @param
     */
    @GetMapping("/detailsForId/{ids}")
    public ExceptionDataVO detailsForId(@PathVariable Long ids) {
        // 实现逻辑
        return exceptionDataService.detailsForId(ids);
    }

    /**
     * 删除放入回收站
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public AjaxResult deleteByIds(@PathVariable Long[] ids){
        exceptionDataService.deleteByIds(ids);
        return AjaxResult.success(true);
    }

    /**
     * 还原回收站
     * @param ids
     * @return
     */
    @GetMapping("/back/{ids}")
    public AjaxResult backByIds(@PathVariable Long[] ids){
        exceptionDataService.backByIds(ids);
        return AjaxResult.success(true);
    }

    @GetMapping("/enter/{ids}")
    public AjaxResult enterByIds(@PathVariable Long[] ids){
        exceptionDataService.enterByIds(ids);
        return AjaxResult.success(true);
    }
    /**
     * 导出模板
     */
    @PostMapping("/exportTemplate")
    public void handleExport(HttpServletResponse response){
        exceptionDataService.handleExport(response);
    }

    /**
     * 导出
     * @param
     * @return
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody List<Long> ids) throws IOException {
        exceptionDataService.export(response,ids);
    }

    @PostMapping("/importData")
    public AjaxResult importData(HttpServletResponse response, MultipartFile file) throws Exception
    {
       exceptionDataService.importExcel(response,file);
        return AjaxResult.success("导入成功");
    }

    /**
     * 获取详情
     * @param
     */
    @GetMapping("/pictureDetails/{ids}")
    public String pictureDetails(@PathVariable Long ids) {
        // 实现逻辑
        return exceptionDataService.pictureDetails(ids);
    }

    //产品计费批量查询
    /**
     * 查询
     * @param
     * @return
     */
    @GetMapping("/getListSku")
    public List<SkuVO> getListSku(String skuAll){
        return exceptionDataService.getListSku(skuAll);
    }

    @PostMapping("/exportSku")
    public void exportSku(HttpServletResponse response,@RequestBody List<SkuVO> skuVo) throws IOException {
        exceptionDataService.exportSku(response,skuVo);
    }

}
